package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.componentdialog.SaturnComponentDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.PageFigure;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.xml.loader.ComponentNodeLoader;


public class SaturnComponentNode extends SaturnBaseComponentNode {

	public static final String SHOW_COMPONENT_NODE_EXCEPTION = "exception";

	public static final String KEY_NODE_COMPONENT = "component";
	public static final String SHOW_NODE_COMPONENT = Messages
			.getString("SaturnComponentNode.component"); //$NON-NLS-1$

	public static final String KEY_COMPONENT_NODE_NAME = "_property_name";

	public static final String KEY_COMPONENT_NODE_KEY = "_property_key";

	public static final String KEY_COMPONENT_NODE_DESCRITPION = "_property_description";

	public static final String KEY_COMPONENT_NODE_PARAMETERS = "_property_parameters";

	public static final String SHOW_COMPONENT_NODE_NAME = "name";

	public static final String SHOW_COMPONENT_NODE_KEY = "key";

	public static final String SHOW_COMPONENT_NODE_DESCRITPION = "description";

	public static final String SHOW_COMPONENT_NODE_PARAMETERS = "parameters";

	private static final long serialVersionUID = 1L;

	private static int number = 1;

	private String key = "";

	private String exception = "";

	private Image image = Activator.getImage(Images.COMPONENT_ICON);

	public SaturnComponentNode() {
		super.setName(SHOW_NODE_COMPONENT + number++);
	}

	public String getException() {
		return exception;
	}

	public void setException(String exception) {
		this.exception = exception;
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		
		PageFigure figure = new PageFigure();
		if (super.isIschanged() == false) {
			Label imageLabel = new Label(image);
			imageLabel.setLocation(new Point(figure.getLocation().x + 90,
					figure.getLocation().y));
			figure.getImageFigure().add(imageLabel);
			Font nameFont = new Font(null, "Arial", 74, SWT.BOLD);
			Label namelabel = new Label(super.getName());
			namelabel.setFont(nameFont);
			figure.getLetterFigure().add(namelabel);
		}

		return figure;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		if (!this.key.equals(key)) {
			this.key = key;
			firePropertyChange(PROPERTERY_CHANGS, null, key);
		}

	}

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node componentNode = SaturnW3CUtil.createChildNode(root,
				KEY_NODE_COMPONENT, null);

		SaturnW3CUtil.createAttribute(componentNode,
				SHOW_COMPONENT_NODE_EXCEPTION, this.exception);
		SaturnW3CUtil.createChildNode(componentNode, SHOW_COMPONENT_NODE_KEY,
				this.key);
		SaturnW3CUtil.createChildNode(componentNode, SHOW_COMPONENT_NODE_NAME,
				super.getName());

		super.createParemeters(componentNode);
		super.creatConfig(componentNode);

		return componentNode;
	}

	public Dialog getDialog() {
		SaturnComponentDialog dialog = new SaturnComponentDialog(
				super.getShell(), this);
		super.reorder(this);
		return dialog;
	}

	@Override
	public String getType() {
		return "SaturnComponentNode";
	}

	@Override
	public String getFullKey() {
		return null;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnComponentNode saturnComponentNode = new SaturnComponentNode();
		SaturnComponentNode saturn = (SaturnComponentNode) shape;
		saturnComponentNode.setLocation(mousePoint);
		saturnComponentNode.setKey(saturn.getKey());
		saturnComponentNode.setName(saturn.getName());
		saturnComponentNode.setDescription(saturn.getDescription());
		saturnComponentNode.setParameters(saturn.getParameters());
		saturnComponentNode.setException(saturn.getException());
		saturnComponentNode.setId(saturn.getId());
		saturnComponentNode.setNextId(saturn.getNextId());
		return saturnComponentNode;
	}

	public List<SaturnParameter> getListParameter(Document document) {
		ComponentNodeLoader loader = new ComponentNodeLoader();
		List<Shape> list = loader.loadNodes(document.getDocumentElement());
		for (Shape shape : list) {
			return shape.getParameters();
		}
		return new ArrayList<SaturnParameter>();
	}

	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"component"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "component",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		Map<String, List<?>> summm = new HashMap<String, List<?>>();
		List<SaturnParameter> parameters = getParameters();
		String name = getName();
		Parameters para = new Parameters();
		String url = "";
		String[] keys = new String[10];
		
		para.setName(getName());
		para.setExperssion(getException());
		para.setId(getId());
		para.setDescription(getDescription());
		
		//创建基础构件的表达式链接
		if (!"".equals(getKey()) && getKey().endsWith(".component")) {
	 		url = getKey().replace(".component", "");
	 		keys = url.split("/");
	 		if (keys[0].equals(tempnames[0] + "::")) {
	 			if (keys[1].equals(tempnames[1])) {
	 				if (keys[2].equals(tempnames[2])) {
	 					url = "../" + keys[3] + "-component/" + keys[3] + "-component.html";
	 				} else {
	 					url = "../../" + keys[2] +"/" + keys[3] + "-logic/" 
	 							+ keys[3] + "-component.html";
	 				}
	 			} else {
	 				url = "../../../" + keys[1] +"/" + keys[2] +"/" 
	 							+ keys[3] + "-component/" + keys[3] + "-component.html";
	 			}
	 		} else {
	 			if (!keys[0].equals("edu.ccut.saturn.basecomponent::")) {
		 			url = "../../../../" + keys[0] + "/" + keys[1] 
		 			            +"/" + keys[2] +"/" + keys[3] + "-component/" + keys[3] + "-component.html";
	 			}
	 		}
	 	}
		para.setExperssion(getKey());
		para.setUrl(url);
	 	basicvaluse.add(para);
	 	
		para.setInfourl( name + "-component.html");
		para.setInfoname( name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.ComponentNode")); //$NON-NLS-1$
		para.setImagename("component");
		summm.put("parameters", parameters);
		summm.put("basicvaluse", basicvaluse);
		String templatePath = "template/export/logic/component";
		String Path = filePath + File.separator + name + "-component.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,summm, Path);
		
		return para;
	}

}